package com.poorstu.manager.impl;

import java.util.ArrayList;
import java.util.List;

import com.common.base.model.PaginationSupport;
import com.common.base.model.PublicFinalData;
import com.poorstu.dao.PowerDao;
import com.poorstu.dao.RoleDao;
import com.poorstu.manager.RoleManager;
import com.poorstu.model.PowerMod;
import com.poorstu.model.RoleMod;

public class RoleManagerImpl implements RoleManager {
	
	private RoleDao roleDao;
	
	private PowerDao powerDao;
	
	
	public boolean delRole(int[] roleIds) {
		List<RoleMod> roleList = new ArrayList<RoleMod>();
		for (int i = 0; i < roleIds.length; i++) {
			RoleMod role = roleDao.getRole(roleIds[i]);
			roleList.add(role);
		}
		return roleDao.delRoleList(roleList);
	}

	public boolean newRole(RoleMod role) {
		return roleDao.newRole(role);
	}

	public PaginationSupport searchAllRoleByPage(int start) {
		
		return roleDao.getRoleByPage(null,null,null,start,PublicFinalData.ROLE_PAGE_MAX_SIZE);
	}

	public boolean updateRole(RoleMod role,String[] powers) {
		
		//获得需要修改的角色对象
		RoleMod roleMod = this.roleDao.getRole(role.getRoleId());
		
		//修改角色对象值
		roleMod.setRoleName(role.getRoleName());
		roleMod.setRoleaStatus(role.getRoleaStatus());
		
		List<PowerMod> powerList = new ArrayList<PowerMod>();
		
		//循环获得页面选择的权限对象
		for (int i = 0; i < powers.length; i++) {
			PowerMod power = powerDao.getPower(Integer.parseInt(powers[i]));
			powerList.add(power);
		}
		roleMod.setPowerList(powerList);
		
		return roleDao.updateRole(roleMod);
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}


	public RoleMod getPowerByRoleId(int roleId) {
		RoleMod role = this.roleDao.getRole(roleId);
		RoleMod newRole = new RoleMod();
		newRole.setRoleId(role.getRoleId());
		newRole.setRoleName(role.getRoleName());
		newRole.setRoleaStatus(role.getRoleaStatus());
		
		List<PowerMod> newPowerList = new ArrayList<PowerMod>();
		List<PowerMod> powerList = this.roleDao.getPowerAll();
		
		for (int i = 0; i < powerList.size(); i++) {
			PowerMod newPower = new PowerMod();
			newPower.setPowerId(powerList.get(i).getPowerId());
			newPower.setPowerName(powerList.get(i).getPowerName());
			if(role.getPowerList().contains(powerList.get(i)))
			{
				newPower.setStruts(1);
			}else{
				newPower.setStruts(0);
			}
			List<PowerMod> newChildPowerList = new ArrayList<PowerMod>();
			for(int j= 0;j<powerList.get(i).getChildPower().size();j++)
			{
				PowerMod newChildPower = powerList.get(i).getChildPower().get(j);
				if(role.getPowerList().contains(newChildPower))
				{
					newChildPower.setStruts(1);
				}else{
					newChildPower.setStruts(0);
				}
				newChildPowerList.add(newChildPower);
			}
			newPower.setChildPower(newChildPowerList);
			newPowerList.add(newPower);
		}
		newRole.setPowerList(newPowerList);
		return newRole;
	}

	public void setPowerDao(PowerDao powerDao) {
		this.powerDao = powerDao;
	}

	public List<RoleMod> selectRoleAll() {
	try{
		List roleList=new ArrayList<RoleMod>();
		roleList=roleDao.selectRoleAll();
		return roleList;
	}catch(Exception e){
		e.printStackTrace();
		return null;
		
	}
		
	}

	public RoleMod selectRoleById(int roleId) {
	   
		return roleDao.getRole(roleId);
	}
	
	

	
	
}
